<?php

class Register extends M_Controller
{

    /**
     *  MB注册
     * 请求登录类型的验证码
     */
    public function registerMember()
    {
        try {
            $data = $this->input->post();
            $code = $this->models('system/sms')->getCaptcha(trim($data['phone']), 'register');
            if ($code <> $data['code']) {
                throw new Exception('验证码错误');
            }
            $this->models('system/sms')->deleteCaptcha(trim($data['phone']), 'register');
            if ($this->models('member')->uidInfo(['username' => $data['phone']], 'uid')) {
                throw new Exception('账号已存在');
            } else {
                $this->db->trans_begin();
                $uid = $this->models('member')->addUser([
                    'username' => $data['phone'],
                    'password' => '123456',
                    'introduce' => '留下您的介绍',
                    'team_code' => $data['team_code']
                ]);
                if ((int)$uid < 1) {
                    throw new Exception('注册失败,稍后重试');
                }
                $salt = substr(md5(rand(0, 999)), 0, 10);
                $password = '123456';
                if (empty($this->models('member')->payPassword([
                    'uid' => $uid,
                    'salt' => $salt,
                    'password' => md5(md5($password) . $salt . md5($password)),
                ]))) {
                    throw new Exception('添加出错 , 稍后重试', 1);
                }
                $this->db->trans_commit();
                apisuccess(1, '注册成功', ['info' => $this->models('member/login')->authcode($uid)]);
            }
        } catch (Exception $e) {
            if ($e->getCode() == 1) {
                $this->db->trans_rollback();
            }
            apisuccess(0, $e->getMessage());
        }
    }

}